package com.ticket.sass.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.admin.mapper.RoleResourceMapMapper;
import com.ticket.sass.admin.service.BaseService;
import com.ticket.sass.common.entity.tenant.RoleResourceMapEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author ywb
 * @date 2022-01-08 15:17
 */
@Repository
public class RoleResourceMapServiceImpl extends ServiceImpl<RoleResourceMapMapper, RoleResourceMapEntity>
    implements BaseService<RoleResourceMapEntity> {
    /**
     * 查询 角色 拥有 的 菜单ids
     */
    public List<Long> resourceIdByRoleId(Long roleId) {
        QueryWrapper<RoleResourceMapEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        queryWrapper.select("resource_id");
        return this.listObjs(queryWrapper, o -> Long.valueOf(o.toString()));
    }
    
    public List<Long> resourceIdByRoles(List<Long> roles) {
        if (Objects.isNull(roles) || roles.size() == 0){
            return new ArrayList<>();
        }
        QueryWrapper<RoleResourceMapEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("role_id", roles);
        queryWrapper.select("resource_id");
        return this.listObjs(queryWrapper, o -> Long.valueOf(o.toString()));
    }

    public List<Long> resourceIdByRole(Long roleId, List<Long> resourceIds) {
        QueryWrapper<RoleResourceMapEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(roleId != null, "role_id", roleId);
        queryWrapper.in( resourceIds != null, "resource_id", resourceIds);
        queryWrapper.select("resource_id");
        return this.listObjs(queryWrapper, o -> Long.valueOf(o.toString()));
    }

    public void delRoleResource(Long roleId, List<Long> resourceIds){
        if (resourceIds == null || resourceIds.size() == 0){
            return;
        }
        QueryWrapper<RoleResourceMapEntity> query = new QueryWrapper<>();
        query.eq(roleId != null, "role_id", roleId);
        query.in("resource_id", resourceIds);
        this.remove(query);
    }
}
